package com.maweis;

import com.intellij.openapi.components.ApplicationComponent;
import com.intellij.openapi.options.Configurable;
import com.intellij.openapi.options.ConfigurationException;
import com.intellij.openapi.ui.Messages;
import com.intellij.openapi.util.*;
import com.intellij.psi.PsiElement;
import org.jdom.Element;
import org.jetbrains.annotations.*;

import javax.swing.*;

/**
 * application component for blog plugin
 *
 * @author peter
 */
public class BlogApplicationComponent implements ApplicationComponent, Configurable, JDOMExternalizable {
    private BlogDefineForm form;
    public String url;
    public String username;
    public String password;

    public BlogApplicationComponent() {
    }

    public void initComponent() {
        // TODO: insert component initialization logic here

    }

    public void disposeComponent() {
        // TODO: insert component disposal logic here
    }

    public void sayHello() {
        // Show dialog with message
        Messages.showMessageDialog(
                url + '-' + username + '-' + password,
                "Sample",
                Messages.getInformationIcon()
        );
    }

    public String getUrl() {
        return url;
    }

    public void setUrl(String url) {
        this.url = url;
    }

    public String getUsername() {
        return username;
    }

    public void setUsername(String username) {
        this.username = username;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    @NotNull
    public String getComponentName() {
        return "BlogApplicationComponent";
    }

    public JComponent createComponent() {
        if (form == null) {
            form = new BlogDefineForm();
        }
        //fill the form with data from component
        form.getDataFrom(this);
        return form.getRootComponent();

    }

    public boolean isModified() {
        return true;
    }

    public void apply() throws ConfigurationException {
        if (form != null) {
            //save form data to component
            form.saveDataTo(this);
        }
    }

    public void reset() {

    }

    public void disposeUIResources() {
        form = null;
    }

    @Nls
    public String getDisplayName() {
        return "Blog Plugin";
    }

    @Nullable
    public Icon getIcon() {
        return BlogConstants.BLOG_LOGO;
    }

    @Nullable
    @NonNls
    public String getHelpTopic() {
        return null;
    }


    public void readExternal(Element element) throws InvalidDataException {
        DefaultJDOMExternalizer.readExternal(this, element);
    }

    public void writeExternal(Element element) throws WriteExternalException {
        DefaultJDOMExternalizer.writeExternal(this, element);
    }
}
